<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Message;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MessageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Message(), function (Grid $grid) {

            $grid->model()->with('user');
            $grid->model()->orderByDesc('id');

            $grid->column('id')->sortable();
            $grid->column('user.tiktokno',trans('user.fields.tiktokno'))->display(function ($var){
                if(!empty($var)){
                    return $this->user_id."({$var})";;
                }else{
                    return "全局";
                }
            });
            $grid->column('content');
            $grid->column('read')->display(function ($var){
                if($this->user_id != 0){
                    if($var == 1){
                        return "已读";
                    }else{
                        return "未读";
                    }

                }
            });
            $grid->column('created_at');
            $grid->quickSearch('user.tiktokno','content','user_id');

            $grid->disableRowSelector();
            $grid->disableFilterButton();
            $grid->disableViewButton();
            $grid->showQuickEditButton();

        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Message(), function (Form $form) {
            $form->select('user_id')->options(User::getUsers());

            $form->text('content');
            //$form->switch('read');

            $form->saving(function (Form $form) {
                if(empty($form->input('user_id'))){
                    $form->user_id = 0;
                }


            });

        });
    }


}
